package com.googlecode.jcaffe.control.modules.settingswindow;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;

import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
final class SettingsWindowView extends JDialog {
	SettingsWindowController controller;
	private JList settingsList;
	private ImageIcon icon;
	private JPanel settingsListPanel;
	private JPanel settingsDetailsPanel;
	private JPanel buttonPanel;
	private JButton exitButton;
	private JPanel databaseSettingsPanel;
	private JLabel hostnameLabel;
	private JTextField hostnameTextField;
	private JLabel portLabel;
	private JTextField portTextField;
	private JLabel usernameLabel;
	private JTextField usernameTextField;
	private JLabel passwordLabel;
	private JPasswordField passwordTextField;
	private JButton saveDatabaseSettingsButton;

	public SettingsWindowView() {
		initializeFrame();
	}

	private void initializeFrame() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("Settings");
		setSize(500, 300);
		setLocationRelativeTo(null);
		setLayout(new MigLayout("", "[fill][grow, fill]", "[fill]"));
		// Added icon to Settings window.
		icon = new ImageIcon(getClass().getResource("/com/googlecode/jcaffe/img/settings.png"));
		setIconImage(icon.getImage());

		settingsListPanel = new JPanel(new MigLayout("insets panel"));
		settingsListPanel.setBorder(BorderFactory.createTitledBorder("Type"));

		Object[] listData = {"Database"};
		settingsList = new JList(listData);
		settingsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		settingsListPanel.add(settingsList, "push, grow, width pref + 5px");
		add(settingsListPanel, "push");

		settingsDetailsPanel = new JPanel(new MigLayout(""));
		settingsDetailsPanel.setBorder(BorderFactory.createTitledBorder("Details"));
		add(settingsDetailsPanel, "push, wrap");

		buttonPanel = new JPanel(new MigLayout("insets 0, align right"));
		exitButton = new JButton("Exit");
		exitButton.setActionCommand("exit");
		buttonPanel.add(exitButton);
		add(buttonPanel, "span");
	}

	void setEventListener(SettingsWindowController controller) {
		this.controller = controller;

		exitButton.addActionListener(this.controller);
		settingsList.addListSelectionListener(this.controller);
	}

	int getSelectedSettingIndex() {
		int selectedListIndex = settingsList.getSelectedIndex();

		return selectedListIndex;
	}

	String getSelectedSettingValue() {
		String settingValue = (String) settingsList.getSelectedValue();

		return settingValue;
	}

	private void removeSettingsFromPanel() {
		int count = settingsDetailsPanel.getComponentCount();

		String settingsPanelName = "";

		if (count > 0) {
			settingsPanelName = settingsDetailsPanel.getComponent(0).getName();
		}

		if (settingsPanelName.equals("database")) {
			hostnameLabel = null;
			hostnameTextField = null;
			portLabel = null;
			portTextField = null;
			usernameLabel = null;
			usernameTextField = null;
			passwordLabel = null;
			passwordTextField = null;
			saveDatabaseSettingsButton = null;

			databaseSettingsPanel = null;
		}

		settingsDetailsPanel.removeAll();
	}

	void displayDatabaseSettings() {
		removeSettingsFromPanel();

		databaseSettingsPanel = new JPanel(new MigLayout("insets 0", "[][grow, push]"));
		databaseSettingsPanel.setName("database");

		hostnameLabel = new JLabel("Hostname:");
		databaseSettingsPanel.add(hostnameLabel, "grow 0");

		hostnameTextField = new JTextField();
		databaseSettingsPanel.add(hostnameTextField, "pushx, growx, wrap");

		portLabel = new JLabel("Port:");
		databaseSettingsPanel.add(portLabel, "grow 0");

		portTextField = new JTextField();
		databaseSettingsPanel.add(portTextField, "pushx, growx, wrap");

		usernameLabel = new JLabel("Username:");
		databaseSettingsPanel.add(usernameLabel, "grow 0");

		usernameTextField = new JTextField();
		databaseSettingsPanel.add(usernameTextField, "pushx, growx, wrap");

		passwordLabel = new JLabel("Password:");
		databaseSettingsPanel.add(passwordLabel, "grow 0");

		passwordTextField = new JPasswordField();
		databaseSettingsPanel.add(passwordTextField, "pushx, growx, wrap");

		saveDatabaseSettingsButton = new JButton("Save");
		saveDatabaseSettingsButton.addActionListener(controller);
		saveDatabaseSettingsButton.setActionCommand("save");
		databaseSettingsPanel.add(saveDatabaseSettingsButton, "pushx, span, gaptop 10, align center");

		settingsDetailsPanel.add(databaseSettingsPanel, "push, grow");

		validate();
	}

	String getHostName() {
		String hostName = hostnameTextField.getText();
		return hostName;
	}
}
